package com.app.conrtoller;

import com.alibaba.fastjson.JSON;
import com.app.domain.ExternalResponse;
import com.app.domain.UploadParameter;
import com.app.domain.YjData;
import com.app.domain.scsb.Grouting;
import com.app.domain.scsb.ScsbResultJson;
import com.app.service.SendMessageService;
import com.app.service.UploadParameterService;
import com.app.service.UploadService;
import com.app.service.YjDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;

/**
 * #铁建5分部3工区
 */
@Slf4j
@RestController
@RequestMapping("/tjwfb")
public class TjwfbUploadController {
    @Value("${yj.tjwfb.serverHost}")
    private String serverHost;
    @Value("${yj.tjwfb.r_DotId}")
    private String r_DotId;
    @Value("${yj.tjwfb.r_sPlaceName}")
    private String r_sPlaceName;
    @Value("${yj.tjwfb.r_machineName}")
    private String r_machineName;

    @Value("${yj.tjwfb2.r_DotId}")
    private String r_DotId2;
    @Value("${yj.tjwfb2.r_sPlaceName}")
    private String r_sPlaceName2;
    @Value("${yj.tjwfb2.r_machineName}")
    private String r_machineName2;
    @Autowired
    YjDataService yjDataService;
    @Autowired
    UploadParameterService uploadParameterService;
    @Autowired
    UploadService uploadService;
    @Autowired
    SendMessageService sendMessageService;

    @RequestMapping("/upload")
    @ResponseBody
    public ExternalResponse upload(@RequestParam(value = "ids[]") String[] ids) throws InterruptedException {
        ExternalResponse externalResponse = null;
        int total = ids.length;
        int succTotal = 0;
        int failTotal = 0;
        StringBuffer responseContent = new StringBuffer();
        responseContent.append("本次共上传数据：【" + total + "】条,");
        UploadParameter uploadParameter = uploadParameterService.findOne("23");
        for (String id : ids) {
            YjData yjData = yjDataService.findOne(id);
            if ("2023062301".equals(yjData.getDevice_code())) {

                Grouting grouting = uploadService.getScsbGroutingInfo(yjData, uploadParameter, r_DotId, r_machineName, r_sPlaceName);
                String s = JSON.toJSONString(grouting);
                ScsbResultJson resultJson = uploadService.sendScsbMessage(serverHost, s);
                if ("success".equals(resultJson.getResult())) {
                    succTotal++;
                    updateState("1", String.valueOf(yjData.getId()));
                } else {
                    updateState("0", String.valueOf(yjData.getId()));
                    failTotal++;
                }
            } else if ("2023091601".equals(yjData.getDevice_code())) {
                Grouting tjwfbGroutingInfo = uploadService.getTjwfbGroutingInfo(yjData, uploadParameter, r_DotId, r_machineName, r_sPlaceName);
                String s = JSON.toJSONString(tjwfbGroutingInfo);
                ScsbResultJson resultJson = uploadService.sendScsbMessage(serverHost, s);
                if ("success".equals(resultJson.getResult())) {
                    succTotal++;
                    updateState("1", String.valueOf(yjData.getId()));
                } else {
                    updateState("0", String.valueOf(yjData.getId()));
                    failTotal++;
                }

            } else if ("2022071801".equals(yjData.getDevice_code())) {
                uploadParameter = uploadParameterService.findOne("32");
                Grouting tjwfbGroutingInfo = uploadService.getTjwfbGroutingInfo(yjData, uploadParameter, r_DotId2, r_machineName2, r_sPlaceName2);
                String s = JSON.toJSONString(tjwfbGroutingInfo);
                ScsbResultJson resultJson = uploadService.sendScsbMessage(serverHost, s);
                if ("success".equals(resultJson.getResult())) {
                    succTotal++;
                    updateState("1", String.valueOf(yjData.getId()));
                } else {
                    updateState("0", String.valueOf(yjData.getId()));
                    failTotal++;
                }
            }
        }
        responseContent.append("成功【" + succTotal + "】条,失败" + failTotal + "条");
        externalResponse = new ExternalResponse(0, responseContent.toString(), "");
        return externalResponse;
    }

    @RequestMapping("/update")
    @ResponseBody
    public String update(@RequestParam("id") String id, @RequestParam("field") String field, @RequestParam("value") String value) {
        if ("theory_volume".equals(field)) {
            yjDataService.ConditionUpdate(id, field, value);
            double parseDouble = Double.parseDouble(value);
            // 获取0到1之间的随机小数
            double randomValue = Math.random();
             // 获取指定范围内的随机小数
            double minValue = 0; // 最小值
            double maxValue = 0.5; // 最大值
            double randomInRange = randomValue * (maxValue - minValue) + minValue;
            double v = parseDouble + randomInRange;
            DecimalFormat df = new DecimalFormat("#.00");
            String result = df.format(v);
            String practical_volume = yjDataService.ConditionUpdate(id, "practical_volume", result);
            if ("修改成功".equals(practical_volume)) {
                return result;
            } else {
                return practical_volume;
            }
        } else {
            return yjDataService.ConditionUpdate(id, field, value);
        }
    }

    private void updateState(String state, String id) {
        yjDataService.updateById(state, id);
    }
}
